#include "CharacterItem.h"

namespace
{
QString CAMPAIGN_FIELD_NAME = "campaign";
}

CharacterItem::CharacterItem(QObject *parent) :
    ItemRoot(parent)
{
}



CharacterItem::CharacterItem(const QString &name, QObject *parent)
    : ItemRoot(name, parent)
{
}



CharacterItem::~CharacterItem()
{
    if (!m_campaignItem.isNull() &&
        !m_campaignItem->parent())
    {
        m_campaignItem->deleteLater();
    }
}



void CharacterItem::setCampaign(CampaignItem* campaign)
{
    m_campaignFile = "";
    if (!m_campaignItem.isNull() && m_campaignItem.data() != campaign)
    {
        m_campaignItem->setParent(0);
        m_campaignItem->deleteLater();
    }

    m_campaignItem = campaign;

    if (m_campaignItem.isNull()) return;
    m_campaignItem->setParent(this);
    m_campaignItem->setData(IdData);

    setData(CampaignData, m_campaignItem->filePath());
}



CampaignItem* CharacterItem::campaign() const
{
    return m_campaignItem.data();
}



QString CharacterItem::campaignFile() const
{
    return data(CampaignData).toString();
}



QString CharacterItem::campaignName() const
{
    if (m_campaignItem.isNull()) return QString::null;

    return m_campaignItem->name();
}



int CharacterItem::fieldCount() const
{
    return (CharacterItem::DataType_LAST - AbstractItem::DataType_FIRST + 1);
}



QString CharacterItem::fieldName(const int &data_type) const
{
    QString name = AbstractItem::fieldName(data_type);
    if (!name.isEmpty()) return name;

    switch (data_type)
    {
    case CampaignData:  return CAMPAIGN_FIELD_NAME;
    default:            return QString::null;
    }
}



int CharacterItem::field(const QString &field_name) const
{
    if (field_name == CAMPAIGN_FIELD_NAME) return CampaignData;

    return ItemRoot::field(field_name);
}



void CharacterItem::setCustomData(const int &data_type, const QVariant &value)
{
    switch (data_type)
    {
    case CampaignData:
        m_campaignFile = value.toString();
        return;
    }

    ItemRoot::setCustomData(data_type, value);
}



QVariant CharacterItem::customData(const int &data_type, int role) const
{
    switch (data_type)
    {
    case CampaignData:
        return m_campaignFile;
    }

    return ItemRoot::customData(data_type, role);
}


